package com.linghui.videoplus.ipai.api.json;


import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.linghui.videoplus.ipai.VideoPlusApplication;
import com.linghui.videoplus.ipai.bean.NickNameBean;
import com.linghui.videoplus.ipai.bean.VideoBean;
import com.linghui.videoplus.ipai.utils.FileUtils;

public class JsonAPI {

//	Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class,new TimestampTypeAdapter()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	
	
	/**
	 * 解析版本信息
	 */
	public HashMap<String,String> analyVersionInfo(String jsonStr){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); 
		Type hashMapType=new TypeToken<HashMap<String ,String>>() {}.getType();
		HashMap<String,String> map=gson.fromJson(jsonStr, hashMapType);
		return map;
	}
	
	/**
	 * 
	 */
	public void saveNickName(Context mContent,NickNameBean nickNameBean){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		String jsonString = gson.toJson(nickNameBean);
		File f=new File(VideoPlusApplication.dataRoot);		
		if(!f.exists()){
			f.mkdirs();
		}
		FileUtils.writeFileData(VideoPlusApplication.nickNameFile, jsonString, mContent);
	}
	/**
	 * 
	 * @return
	 */
	public String getNickName(){
		String name=null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		String json=FileUtils.readFileData(VideoPlusApplication.nickNameFile);
		NickNameBean nickNameBean=gson.fromJson(json, NickNameBean.class);
		name=nickNameBean.getNickName();
		return name;
	}
	
	
	/**
	 * 获取我的影集
	 * @return
	 */
	public List<VideoBean> getMyVideo(boolean isDesc){
		Type listType = new TypeToken<List<VideoBean>>(){}.getType();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		String json=FileUtils.readFileData(VideoPlusApplication.videoFile);
		List<VideoBean> videoList=gson.fromJson(json, listType);
		List <VideoBean> list=null;
		
		if(isDesc){
			if(videoList!=null && !videoList.isEmpty()){
				list=new ArrayList<VideoBean> ();
				for(int i=videoList.size()-1;i>=0;i--){
					list.add(videoList.get(i));
				}
			}
		}else{
			list=videoList;
		}
		return list;
	}
	
	/**
	 * 保存我的影集
	 * @param mContent
	 * @param videoBean
	 */
	public void saveMyVideo(Context mContent,VideoBean videoBean){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		List<VideoBean> videoList=getMyVideo(false);
		if(videoList==null){
			videoList=new ArrayList<VideoBean>();
		}
		videoList.add(videoBean);
		String jsonString=gson.toJson(videoList);
		File f=new File(VideoPlusApplication.dataRoot);		
		if(!f.exists()){
			f.mkdirs();
		}
		FileUtils.writeFileData(VideoPlusApplication.videoFile, jsonString, mContent);
	}
	
	
	/**
	 * 删除
	 * @param mContent
	 * @param videoBean
	 */
	public void deleteMyVideo(Context mContent,VideoBean videoBean){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		List<VideoBean> list=getMyVideo(false);
		String id=videoBean.getId();
		for (VideoBean srcVideo : list) {
			if(id.equals(srcVideo.getId())){
				list.remove(srcVideo);
				break;
			}
		}
		String jsonString=gson.toJson(list);
		File f=new File(VideoPlusApplication.dataRoot);		
		if(!f.exists()){
			f.mkdirs();
		}
		FileUtils.writeFileData(VideoPlusApplication.videoFile, jsonString, mContent);
		
		File deleteFile=new File(videoBean.getVideoPath());
		if(deleteFile.exists()){
			deleteFile.delete();
		}
	}
		
		
	
	
	/**
	 * 更新我的视频
	 * @param mContent
	 * @param videoBean
	 */
	public void updateMyVideo(Context mContent,VideoBean videoBean){
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		List<VideoBean> list=getMyVideo(false);
		String id=videoBean.getId();
		for (VideoBean srcVideo : list) {
			if(id.equals(srcVideo.getId())){
				list.remove(srcVideo);
				list.add(videoBean);
				break;
			}
		}
		String jsonString=gson.toJson(list);
		File f=new File(VideoPlusApplication.dataRoot);		
		if(!f.exists()){
			f.mkdirs();
		}
		FileUtils.writeFileData(VideoPlusApplication.videoFile, jsonString, mContent);
	}
}
